package com.alog.yzd.jdbc.hm;

import com.mysql.cj.result.Row;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DaoTemplate {
    protected Object find(String sql, List<Object> params, RowMappepr mappepr){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils2.getConnction();
            ps = conn.prepareStatement(sql);
            int index = 1;
            if(params != null && !params.isEmpty()){
                for(Object obj : params){
                    ps.setObject(index++, obj);
                }
            }
            rs = ps.executeQuery();
            Object obj = null;
            while (rs.next()){
                obj = mappepr.map(rs);
            }
            return obj;
        } catch(SQLException e){
            throw new DaoException(e.getMessage(), e);
        } finally {
            JdbcUtils2.release(rs, ps, conn);
        }
    }
}
